import { createRouter, createWebHashHistory } from "vue-router";
import Login from "../Login.vue";
import Home from "../Home.vue";
import Error from "../Error.vue";

const routes = [
  {
    path: "/",
    name: "Login",
    component: Login
  },
  {
    path: "/home",
    name: "Home",
    meta: { label: "Home" },
    component: Home
  },
  {
    path: "/error",
    name: "Error",
    meta: { label: "Error" },
    component: Error
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  const account = localStorage.getItem("account");
  if (to.name !== "Login" && !account) {
    next({ name: "Login" });
  } else if (to.name === "Login" && account) {
    next({ name: "Home" });
  } else {
    next();
  }
});

export default router;
